package com.shanxia.codec.cmd;

import com.shanxia.codec.ProtocolDataBase;
import com.shanxia.codec.enums.DecodeEnum;
import lombok.Data;

import java.util.Arrays;
import java.util.Objects;

import static com.shanxia.codec.CodeConstant.ONEBYTE_ADDRS;
import static com.shanxia.codec.Decoder.*;

/**
 * C -> S
 * 充电桩回复服务器下发擦除指令
 */
@Data
public class Cmd1002 extends ProtocolDataBase {

    public static final int MIN_SIZE = 1;

    private int progress;               // 擦除完成百分比

    public Cmd1002(){
        super.setCmd(1002);
    }

    public static Cmd1002 decodeFromBase(ProtocolDataBase protocolDataBase) {
        Cmd1002 cmd = new Cmd1002();
        cmd.copyDataFrom(protocolDataBase);
        byte[] dataField = protocolDataBase.getDataField();
        if (dataField.length < MIN_SIZE) {
            cmd.setResult(DecodeEnum.InvalidData);
            return cmd;
        }
        cmd.progress = getUint8(dataField[0]);
        return cmd;
    }

    @Override
    public String toString() {

        StringBuffer buffer = new StringBuffer();
        buffer.append("#cmd1002#").append("\n");
        buffer.append("擦除完成百分比:").append(progress).append("\n");
        return buffer.toString();
    }
}
